import _ from 'lodash'

/* 4到16位（字母，数字，下划线，减号） */
export function isUserName (str) {
  return str.length > 3 && /^[a-zA-Z0-9_-]{4,16}$/.test(str)
}

/* 真是姓名应该不包含数字，字符长度 > 1 */
export function isRealName (str) {
  return str.length > 1 && /^(\w|[\u4e00-\u9fa5])+$/.test(str)
}

/* 合法url */
export function isURL (str) {
  const urlRE = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlRE.test(str)
}

/* 手机号 */
export function isMobile (str) {
  return /^1\d{10}$/.test(str)
}

/* qq号 */
export function isQQ (str) {
  return /^[1-9]\d{4,9}$/.test(str)
}

/* email */
export function isEmail (str) {
  return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str)
}

/* postCode */
export function isPostCode (str) {
  return /^[1-9]\d{5}(?!\d)$/.test(str)
}

/* idCard */
export function isIDCard (str) {
  return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str)
}

export function isAddress (str) {
  return /^.{2,}$/.test(str.trim())
}

/**
 * 验证表单
 */

const isEmptyString = (str) => {
  return str == null || str === ''
}

/**
 * 验证流程
 * 目前只验证rule.type === 'string'
 * @config [object]
 * */
const validateMethod = (rule, str, cb, config) => {
  const NO_EMPTY = '不能为空'
  let defConfig = {
    name: '该字段', // 字段名
    validator: () => true, // 验证方法，默认返回true, true 验证通过 false 不通过
    message: (config.name || '该字段') + '格式不正确' // 默认验证不匹配提示
  }
  let _config = _.assign(defConfig, (config || {}))
  if (rule.type === 'string') {
    if (isEmptyString(str)) {
      if (rule.required) {
        return cb(new Error(_config.name + NO_EMPTY))
      }
    } else {
      if (!_config.validator(str)) {
        return cb(new Error(_config.message))
      }
    }
  } else if (rule.type === 'array') {
    if (_.isEmpty(str)) {
      if (rule.required) {
        return cb(new Error(_config.name + NO_EMPTY))
      }
    }
  }
  return cb()
}

export function validateUserName (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '用户名',
    validator: isUserName,
    message: '用户名为4到16位（只能是字母，数字，下划线，减号）'
  })
}

export function validateRealName (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '姓名',
    validator: isRealName,
    message: '姓名不能包含数字，最少2个字符'
  })
}

export function validateMobile (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '手机号',
    validator: isMobile,
    message: '手机号只能是1开头的11为数字'
  })
}

export function validateQQ (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: 'QQ号',
    validator: isQQ,
    message: 'QQ号为5-10位数字'
  })
}

export function validateEmail (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '邮箱',
    validator: isEmail,
    message: '邮箱格式不正确，如：xx@xx.xx'
  })
}

export function validatePostCode (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '邮编',
    validator: isPostCode,
    message: '邮编为1-9开头的6位数字'
  })
}

export function validateIDCard (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '身份证',
    validator: isIDCard,
    message: '身份证格式不正确'
  })
}

export function validateURL (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '网址',
    validator: isURL
  })
}

export function validateAddress (rule, str, cb) {
  validateMethod(rule, str, cb, {
    name: '地址',
    validator: isAddress,
    message: '地址最少2个字符'
  })
}
